package com.aizhixin.lab.docker.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.docker.common.AuthUtilService;
import com.aizhixin.lab.docker.common.ExceptionAuth;
import com.aizhixin.lab.docker.common.ExceptionValidation;
import com.aizhixin.lab.docker.common.ReturnJsonData;
import com.aizhixin.lab.docker.core.ContainerConfig;
import com.aizhixin.lab.docker.domain.FormData;
import com.aizhixin.lab.docker.domain.Image;
import com.aizhixin.lab.docker.domain.RContainers;
import com.aizhixin.lab.docker.entity.ImageInfo;
import com.aizhixin.lab.docker.repository.ImageInfoRepository;
import com.aizhixin.lab.docker.service.ChapterImagesService;
import com.aizhixin.lab.docker.service.DockerImageService;
import com.aizhixin.lab.docker.util.GetTokenUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/docker/Image")
@Api(value = "docker镜像管理api", description = "docker镜像管理")
public class DockerImageController {
    @Autowired
    private AccountService authUtilService;
    @Autowired
    private DockerImageService dockerImageService;
    @Autowired
    private ChapterImagesService chapterImagesService;
    @Autowired
    private ImageInfoRepository imageInfoRepository;
    @Autowired
    private ContainerConfig containerConfig;

    @RequestMapping(value = "/likeSearch", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "模糊搜索镜像", httpMethod = "GET", notes = "模糊搜索镜像<br><br><b>lwq</b>")
    public ReturnJsonData likeSearch(@RequestHeader("Authorization") String token,
                                              @ApiParam(value = "name", required = false) @RequestParam(value = "name") String name) {
        Map<String, Object> result = new HashMap<String, Object>();
        AccountDTO accountDTO = authUtilService.getUserByToken(token);
        if (accountDTO == null) {
            throw new ExceptionAuth(401, "无权限");
        }
        token = GetTokenUtils.getToken(accountDTO);
        List<Image> imageInfos = dockerImageService.imageInfoList(name);
        ReturnJsonData returnJsonData = new ReturnJsonData<>();
        returnJsonData.setCode(200);
        returnJsonData.setData(imageInfos);
        return returnJsonData;
    }
    @RequestMapping(value = "/getList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "镜像列表", httpMethod = "POST", notes = "镜像列表<br><br><b>lwq</b>")
    public ReturnJsonData getList(@RequestHeader("Authorization") String token,
                                  @ApiParam(value = "creator") @RequestParam(value = "creator", required = false) String creator) {
        Map<String, Object> result = new HashMap<String, Object>();
        AccountDTO accountDTO = authUtilService.getUserByToken(token);
        if (accountDTO == null) {
            throw new ExceptionAuth(401, "无权限");
        }
        token = GetTokenUtils.getToken(accountDTO);
        FormData imageList = dockerImageService.getImageList(token, creator,accountDTO.getId());
        if (imageList.getErrno() != 0) {
            throw new ExceptionValidation(416, imageList.getErrmsg());
        }
        ReturnJsonData returnJsonData = new ReturnJsonData<>();
        returnJsonData.setCode(200);
        returnJsonData.setData(imageList.getData());
        return returnJsonData;
    }

    @RequestMapping(value = "/getImageInformation", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取镜像详情", httpMethod = "GET", notes = "获取镜像详情<br><br><b>lwq</b>")
    public ReturnJsonData getImageInformation(@RequestHeader("Authorization") String token,
                                              @ApiParam(value = "imageName", required = false) @RequestParam String imageName) {
        Map<String, Object> result = new HashMap<String, Object>();
        AccountDTO accountDTO = authUtilService.getUserByToken(token);
        if (accountDTO == null) {
            throw new ExceptionAuth(401, "无权限");
        }
        token = GetTokenUtils.getToken(accountDTO);
        if (StringUtils.isEmpty(imageName)) {
            throw new ExceptionValidation(416, "imageName is null");
        }
        FormData imageList = dockerImageService.getAppointImage(token, 1, imageName);
        if (imageList.getErrno() != 0) {
            throw new ExceptionValidation(416, imageList.getErrmsg());
        }
        ReturnJsonData returnJsonData = new ReturnJsonData<>();
        returnJsonData.setCode(200);
        returnJsonData.setData(imageList.getData());
        return returnJsonData;
    }


    @RequestMapping(value = "/editImageInformation", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "编辑镜像", httpMethod = "POST", notes = "编辑镜像<br><br><b>lwq</b>")
    public ReturnJsonData editImageInformation(@RequestHeader("Authorization") String token,
                                                  @ApiParam(value = "name", required = false) @RequestParam(value = "name", required = true) String name,
                                                  @ApiParam(value = "desc", required = false) @RequestParam(value = "desc", required = true) String desc) {
        Map<String, Object> result = new HashMap<String, Object>();
        AccountDTO accountDTO = authUtilService.getUserByToken(token);
        if (accountDTO == null) {
            throw new ExceptionAuth(401, "无权限");
        }
        token = GetTokenUtils.getToken(accountDTO);
        if (StringUtils.isEmpty(name)) {
            throw new ExceptionValidation(416, "name is null");
        }
        if (StringUtils.isEmpty(desc)) {
            throw new ExceptionValidation(416, "desc is null");
        }
        token = GetTokenUtils.getToken(accountDTO);
        FormData imageList = dockerImageService.editImage(token, name, desc);
        if (imageList.getErrno() != 0) {
            throw new ExceptionValidation(416, imageList.getErrmsg());
        }
        ReturnJsonData returnJsonData = new ReturnJsonData<>();
        returnJsonData.setCode(200);
        returnJsonData.setData(imageList.getData());
        return returnJsonData;
    }
    @RequestMapping(value = "/updateImageInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "更新镜像描述", httpMethod = "GET", notes = "更新镜像描述<br><br><b>lwq</b>")
    public ReturnJsonData updateImageInfo(@RequestHeader("Authorization") String token,
                                               @ApiParam(value = "imageName", required = false) @RequestParam(value = "imageName", required = false) String imageName,
                                               @ApiParam(value = "desc", required = false) @RequestParam(value = "desc", required = false) String desc) {
        Map<String, Object> result = new HashMap<String, Object>();
        AccountDTO accountDTO = authUtilService.getUserByToken(token);
        if (accountDTO == null) {
            throw new ExceptionAuth(401, "无权限");
        }
        if (StringUtils.isEmpty(imageName)) {
            throw new ExceptionValidation(417, "name is null");
        }
        if (StringUtils.isEmpty(desc)) {
            throw new ExceptionValidation(417, "desc is null");
        }
        ImageInfo info = imageInfoRepository.findByImagesName(imageName);
        if (info!=null){
            info.setImageDesc(desc);
            imageInfoRepository.save(info);
        }else {
            info=new ImageInfo();
            info.setImageDesc(desc);
            info.setImagesName(imageName);
            info.setVmNum(1);
            info.setCreateBy(4L);
            imageInfoRepository.save(info);
        }
        ReturnJsonData returnJsonData = new ReturnJsonData<>();
        returnJsonData.setCode(200);
        returnJsonData.setData("success");
        return returnJsonData;
    }
    @RequestMapping(value = "/updateImage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改镜像配置信息", httpMethod = "POST", notes = "修改镜像配置信息<br><br><b>lwq</b>")
    public ReturnJsonData updateImageVmNum(@RequestHeader("Authorization") String token,
                                          @ApiParam(value = "ImageInfo", required = false) @RequestBody ImageInfo imageInfo) {
        Map<String, Object> result = new HashMap<String, Object>();
        AccountDTO accountDTO = authUtilService.getUserByToken(token);
        if (accountDTO == null) {
            throw new ExceptionAuth(401, "无权限");
        }
        if (StringUtils.isEmpty(imageInfo.getImagesName())) {
            throw new ExceptionValidation(416, "name is null");
        }

        ImageInfo info = imageInfoRepository.findByImagesName(imageInfo.getImagesName());
        if (info!=null){
           info.setVmNum(imageInfo.getVmNum());
           info.setExpire( imageInfo.getExpire());
           info.setOverSold(imageInfo.getOverSold());
           info.setExpire(imageInfo.getExpire());
           info.setSpec(imageInfo.getSpec());
            imageInfoRepository.save(info);
        }else {
            info=new ImageInfo();
            BeanUtils.copyProperties(imageInfo,info);
            info.setVmNum(imageInfo.getVmNum());
            info.setImageDesc("describe image"+imageInfo.getImagesName());
            info.setImagesName(info.getImagesName());
            info.setCreateBy(4L);
            imageInfoRepository.save(info);
        }
        ReturnJsonData returnJsonData = new ReturnJsonData<>();
        returnJsonData.setCode(200);
        returnJsonData.setData("success");
        return returnJsonData;
    }
    @RequestMapping(value = "/deleteImage", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除镜像", httpMethod = "DELETE", notes = "删除镜像<br><br><b>lwq</b>")
    public ReturnJsonData deleteImage(@RequestHeader("Authorization") String token,
                                  @ApiParam(value = "imageName") @RequestParam(value = "imageName", required = false) String imageName) {
        Map<String, Object> result = new HashMap<String, Object>();
        AccountDTO accountDTO = authUtilService.getUserByToken(token);
        if (accountDTO == null) {
            throw new ExceptionAuth(401, "无权限");
        }
        if (StringUtils.isEmpty(imageName)) {
            throw new ExceptionAuth(417, "imageName is null");
        }
        token = GetTokenUtils.getToken(accountDTO);
        FormData imageList = dockerImageService.deleteImage(token, imageName);
        if (imageList.getErrno() != 0) {
            throw new ExceptionValidation(416, imageList.getErrmsg());
        }

        ReturnJsonData returnJsonData = new ReturnJsonData<>();
        returnJsonData.setCode(200);
        returnJsonData.setData(imageList.getData());
        return returnJsonData;
    }

    @RequestMapping(value = "/getContainerExpire", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取容器过期时间", httpMethod = "POST", notes = "获取容器过期时间<br><br><b>lwq</b>")
    public ReturnJsonData getContainerExpire(@RequestHeader("Authorization") String token) {
        Map<String, Object> result = new HashMap<String, Object>();
        AccountDTO accountDTO = authUtilService.getUserByToken(token);
        if (accountDTO == null) {
            throw new ExceptionAuth(401, "无权限");
        }
        token = GetTokenUtils.getToken(accountDTO);
        ReturnJsonData returnJsonData = new ReturnJsonData<>();
        returnJsonData.setCode(200);
        returnJsonData.setData(containerConfig.getExpire());
        return returnJsonData;
    }
    @RequestMapping(value = "/setContainerExpire", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "设置容器过期时间", httpMethod = "GET", notes = "设置容器过期时间<br><br><b>lwq</b>")
    public ReturnJsonData setContainerExpire(@RequestHeader("Authorization") String token,
                                           @RequestParam Long expire) {
        Map<String, Object> result = new HashMap<String, Object>();
        AccountDTO accountDTO = authUtilService.getUserByToken(token);
        if (accountDTO == null) {
            throw new ExceptionAuth(401, "无权限");
        }
        containerConfig.setExpire(expire);
        token = GetTokenUtils.getToken(accountDTO);
        ReturnJsonData returnJsonData = new ReturnJsonData<>();
        returnJsonData.setCode(200);
        returnJsonData.setData(containerConfig.getExpire());
        return returnJsonData;
    }




}
